package com.ayf.payment.game.api.service.payment.ayb.util;

import com.ayf.payment.game.api.utils.SM2Utils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

public class AybSignUtil {

    public static String getSign(String traceId, String methodName, Map<String,String> reqParam,String apiKey,String merchantPriKey) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : reqParam.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue() + "&");
        }
        String signOriginStr = sb.toString();
        signOriginStr=signOriginStr.concat("key=").concat(apiKey);
        LogPortal.info("{} {} 签名原文:{}",traceId,methodName,signOriginStr);
        String sign = SM2Utils.sign(merchantPriKey,signOriginStr);
        if (StringUtils.isEmpty(sign)) {
            return null;
        }
        LogPortal.info("{} {} 实际签名:{}",traceId,methodName,sign);
        return sign;
    }


    /**
     * 异步通知验签
     * @param channelPubKey
     * @param content
     * @param channelSign
     * @return
     */
    public static boolean notifyVerify(String channelPubKey, String content, String channelSign){
        return SM2Utils.verify(channelPubKey,content,channelSign);
    }


    /**
     * 获取签名串
     * @param reqParam
     * @param apiKey
     * @return
     */
    public static String getSignStr(String traceId,String methodName,Map<String,String> reqParam,String apiKey) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : reqParam.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue() + "&");
        }
        String signOriginStr = sb.toString();
        signOriginStr=signOriginStr.concat("key=").concat(apiKey);
        LogPortal.info("{} {} 签名原文:{}",traceId,methodName,signOriginStr);
        return signOriginStr;
    }


}
